<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>客户编辑</title>
<link href="/resources/lib/ligerUI/skins/Aqua/css/ligerui-all.css" rel="stylesheet" type="text/css" />
<link href="/resources/lib/ligerUI/skins/ligerui-icons.css" rel="stylesheet" type="text/css" />
<script src="/resources/lib/jquery/jquery-1.3.2.min.js" type="text/javascript"></script>
<script src="/resources/lib/ligerUI/js/ligerui.all.js" type="text/javascript"></script>
<!-- <script src="/resources/lib/ligerUI/js/plugins/ligerForm.js" type="text/javascript"></script> -->
<script src="/resources/lib/jquery-validation/jquery.validate.min.js" type="text/javascript"></script> 
<script src="/resources/lib/jquery-validation/jquery.metadata.js" type="text/javascript"></script>
<script src="/resources/lib/jquery-validation/messages_cn.js" type="text/javascript"></script>
<script src="/resources/lib/json2.js" type="text/javascript"></script>
<script src="/resources/js/jsF7.js" type="text/javascript"></script>
<script src="/resources/js/selectData.js" type="text/javascript"></script>
<script src="/resources/js/themis.js" type="text/javascript"></script>
<script src="/resources/js/validation.js" type="text/javascript"></script>

<style type="text/css">
    .l-table-edit {}
    .l-table-edit-td-label{width: 100px;padding-top: 5px;}
    .l-table-edit-td-input{width: 200px;padding-top: 5px;}
    .div-buttom{position: absolute;top:380px;}
    
</style>
<script type="text/javascript">
//是否选择                   

var form1 = null;
var grid = null;
var toolbar = null;
var info = '${info}';
var existCustomer = null;
if(info){
	existCustomer = JSON.parse(info); 
}
var status = '${status}';
var number = '${number}';
var matCategoryUuid ='${matCategoryUuid}';

 
$(function (){
    //工具条
    toolbar = $("#toptoolbar").ligerToolBar({ items: [
        { text: '保存', id:'save',click: saveForm,icon: 'save' }
    ]
    });
  //创建表单(头部)
    form1 = $("#form1").ligerForm({
    inputWidth: 170, labelWidth: 90, space: 40,
    validate:true,
    fields: [
        { name: "uuid", type: "hidden" },
        { name: "createUser.uuid", type: "hidden" },
        { name: "createDate", type: "hidden" },
        { display: "编号", name: "number", type: "text",newline: false, validate: {remote:{type:"POSt",url:"/material/checkNumberUsed",
        	  data:{uuid:function(){
            	  var uuidEd = form1.getEditor("uuid"); //form中应用
                  var uuid = uuidEd.val();
            	  return uuid;
            	  }}},required: true,minlength: 1},validateMessage:{remote:"此编码已经存在"}},
        { display: "名称", name: "name", type: "text",newline: false,  validate: { required: true} },
       /*  { display: "组合商品", name: "isGroupM", type: "select",newline: false,editor: {type:"combobox",data:bool_data,cancelable:false,initValue:'0',initText :"否"}}, */
       { display: "商品条码", name: "barCode", newline: false, type: "text"}, 
       { display: "规格型号", name: "standard", newline: false, type: "text"},
        { display: "商品分类", name: "matCategory.uuid", newline: false, type: "popup",textField:"matCategory.name",validate: { required: true},
     	   editor: {
                grid: getCategoriesGrid(true),
                valueField:'uuid',
                textField: 'name',
                parms :{leaf:'1',date:new Date().getTime()},
                width: 300,
                height:100,
                realTimeLoad:true
       	 }
        },
        { display: "计量单位", name: "baseUnit.uuid", newline: false, type: "popup",textField:"baseUnit.name",validate: { required: true},
     	   editor: {
                grid: getBaseUnitMeasureGrid(true),
                valueField:'uuid',
                textField: 'name',
                parms :{date:new Date().getTime()},
                width: 300,
                height:100,
                realTimeLoad:true
       	 }   
        },
        { display: "安全库存", name: "safeStock",  minWidth: 100, type: "spinner",validate: { required: true},editor:{type: 'int',isNegative:false}},
        { display: "最小采购量", name: "minPurchaseAmount",  minWidth: 100, type: "spinner",validate: { required: true},editor:{type: 'int',isNegative:false}},
        { display: "商品图片", name: "matPicture", newline: false, type: "text"},
        
        
        
       /*  { display: "精度", name: "pricePrecision", newline: false, type: "spinner",editor:{type: 'int',isNegative:false}}, */
        { display: "批次管理", name: "lotManage", newline: false,type: "select",editor: {data:true_false_data,cancelable:false,initValue:"false",valueField: 'value',textField:'text'}}, 
			{ display: "条码管理", name: "barCodeManage", newline: false,  type: "select",editor: {data:true_false_data,cancelable:false,initValue:"true",valueField: 'value',textField: 'text',
				onSelected  :function(value,text){
					if(form1){
						if(value == "false"){
							 var barCode = form1.getEditor("barCode"); //form中应用
							 barCode.setValue(null);
							 form1.setFieldValidate("barCode",{required:false},{});
	                    	  
	                    	  form1.setEnabled(["barCode"],false);
						}else if(value == "true"){
							form1.setFieldValidate("barCode",{required: true},{});
							 form1.setEnabled(["barCode"],true);
						}
					}
				}
			}}, 
			{ display: "序列号管理", name: "sequeManage", newline: false,  type: "select",editor: {data:true_false_data,cancelable:false,initValue:"false",valueField: 'value',textField: 'text'}}, 
			{ display: "保质期管理", name: "expiraDateManage", newline: false,  type: "select",editor: {data:true_false_data,cancelable:false,initValue:"false",valueField: 'value',textField: 'text',
				onSelected  :function(value,text){
					if(form1){
						if(value == "false"){
							 var expiraData = form1.getEditor("expiraData"); //form中应用
							 expiraData.setValue(null);
							 form1.setFieldValidate("expiraData",{required:false},{});
	                    	  var expiraDataUtil = form1.getEditor("expiraDataUtil"); //form中应用
	                    	  expiraDataUtil.setText(null);
	                    	  expiraDataUtil.setValue(null);
	                    	  form1.setFieldValidate("expiraDataUtil",{required:false},{});
	                    	  form1.setEnabled(["expiraDataUtil","expiraData"],false);
						}else if(value == "true"){
							form1.setFieldValidate("expiraData",{required: true},{});
							form1.setFieldValidate("expiraDataUtil",{required: true},{});
							 form1.setEnabled(["expiraDataUtil","expiraData"],true);
						}
					}
				}
			}}, 
			/* { display: "商品分类", name: "inventory.matCategory", newline: false, type: "text"},  */
			/* { display: "库存计量单位", name: "inventory.invenUtil", newline: false, type: "text"},  */
			{ display: "保质期", name: "expiraData", newline: false, type: "spinner",editor:{isNegative:false}},
			{ display: "保质期单位", name: "expiraDataUtil", newline: false,  type: "select",editor: {data:shelf_life_unit,
				valueField: 'value',textField: 'text'
			}}
        ]/* ,
        tab: {
            items : [
                {
                    title: '基本信息', fields: [
                           { display: "规格型号", name: "standard", newline: false, type: "text"},
                           { display: "商品分类", name: "matCategory.uuid", newline: false, type: "popup",textField:"matCategory.name",
                        	   editor: {
			                       grid: getCategoriesGrid(true),
			                       valueField:'uuid',
			                       textField: 'name',
			                       parms :{date:new Date().getTime()},
			                       width: 300,
			                       height:100,
			                       realTimeLoad:true
			              	 }
                           },
                           { display: "基本计量单位", name: "baseUnit.uuid", newline: false, type: "popup",textField:"baseUnit.name",
                        	   editor: {
			                       grid: getBaseUnitMeasureGrid(true),
			                       valueField:'uuid',
			                       textField: 'name',
			                       parms :{date:new Date().getTime()},
			                       width: 300,
			                       height:100,
			                       realTimeLoad:true
			              	 }   
                           },
                           { display: "精度", name: "pricePrecision", newline: false, type: "spinner",editor:{type: 'int',isNegative:false}},
                           { display: "商品图片", name: "matPicture", newline: false, type: "text"},
                           { display: "批次管理", name: "inventory.lot", newline: false,type: "select",editor: {data:bool_data,cancelable:false,initValue:"0",valueField: 'value'}}, 
							{ display: "条码管理", name: "inventory.barCode", newline: false,  type: "select",editor: {data:bool_data,cancelable:false,initValue:"0",valueField: 'value',textField: 'text'}}, 
							{ display: "序列号管理", name: "inventory.seque", newline: false,  type: "select",editor: {data:bool_data,cancelable:false,initValue:"0",valueField: 'value',textField: 'text'}}, 
							{ display: "保质期管理", name: "inventory.expiraDateManer", newline: false,  type: "select",editor: {data:bool_data,cancelable:false,initValue:"0",valueField: 'value',textField: 'text',
								onSelected  :function(value,text){
									if(form1){
										if(value == "0"){
											 var expiraData = form1.getEditor("inventory.expiraData"); //form中应用
											 expiraData.setValue(null);
											 form1.setFieldValidate("inventory.expiraData",{required:false},{});
					                    	  var expiraDataUtil = form1.getEditor("inventory.expiraDataUtil"); //form中应用
					                    	  expiraDataUtil.setText(null);
					                    	  expiraDataUtil.setValue(null);
					                    	  form1.setFieldValidate("inventory.expiraDataUtil",{required:false},{});
					                    	  form1.setEnabled(["inventory.expiraDataUtil","inventory.expiraData"],false);
										}else if(value == "1"){
											form1.setFieldValidate("inventory.expiraData",{required: true},{});
											form1.setFieldValidate("inventory.expiraDataUtil",{required: true},{});
											 form1.setEnabled(["inventory.expiraDataUtil","inventory.expiraData"],true);
										}
									}
								}
							}}, 
							 { display: "商品分类", name: "inventory.matCategory", newline: false, type: "text"},  
							 { display: "库存计量单位", name: "inventory.invenUtil", newline: false, type: "text"}, 
							{ display: "保质期", name: "inventory.expiraData", newline: false, type: "spinner",editor:{isNegative:false}},
							{ display: "保质期单位", name: "inventory.expiraDataUtil", textField:"inventory.expiraDataUtil",newline: false,  type: "select",editor: {data:shelf_life_unit,
								valueField: 'value',textField: 'text'
							}}
                    ]
                } ,
                {
                    title: '库存资料', fields: [
							 { display: "所属公司编码", name: "inventory.suCompanyUuid", type: 'popup',newline: false,textField:'inventory.suCompanyNum',validate: { required: true, minlength:1 },
				              	 editor: {
				                       grid: getDepGroupGrid(true),
				                       valueField:'uuid',
				                       textField: 'number',
				                       parms :{date:new Date().getTime()},
				                       width: 300,
				                       height:100,
				                       realTimeLoad:true,
				                       onSelected:function(e){
				                     	  var edd = form1.getEditor("inventory.suCompanyName"); //form中应用
				                     	  edd.setValue(e.data[0].name);
				                       }
				              	 }
				            },
							{ display: "所属公司名称", name: "inventory.suCompanyName", newline: false, type: "text",},  
							{ display: "批次管理", name: "inventory.lot", newline: false,type: "select",editor: {data:bool_data,cancelable:false,initValue:"0",valueField: 'value'}}, 
							{ display: "条码管理", name: "inventory.barCode", newline: false,  type: "select",editor: {data:bool_data,cancelable:false,initValue:"0",valueField: 'value',textField: 'text'}}, 
							{ display: "序列号管理", name: "inventory.seque", newline: false,  type: "select",editor: {data:bool_data,cancelable:false,initValue:"0",valueField: 'value',textField: 'text'}}, 
							{ display: "保质期管理", name: "inventory.expiraDateManer", newline: false,  type: "select",editor: {data:bool_data,cancelable:false,initValue:"0",valueField: 'value',textField: 'text',
								onSelected  :function(value,text){
									if(form1){
										if(value == "0"){
											 var expiraData = form1.getEditor("inventory.expiraData"); //form中应用
											 expiraData.setValue(null);
											 form1.setFieldValidate("inventory.expiraData",{required:false},{});
					                    	  var expiraDataUtil = form1.getEditor("inventory.expiraDataUtil"); //form中应用
					                    	  expiraDataUtil.setText(null);
					                    	  expiraDataUtil.setValue(null);
					                    	  form1.setFieldValidate("inventory.expiraDataUtil",{required:false},{});
					                    	  form1.setEnabled(["inventory.expiraDataUtil","inventory.expiraData"],false);
										}else if(value == "1"){
											form1.setFieldValidate("inventory.expiraData",{required: true},{});
											form1.setFieldValidate("inventory.expiraDataUtil",{required: true},{});
											 form1.setEnabled(["inventory.expiraDataUtil","inventory.expiraData"],true);
										}
									}
								}
							}}, 
							 { display: "商品分类", name: "inventory.matCategory", newline: false, type: "text"},  
							 { display: "库存计量单位", name: "inventory.invenUtil", newline: false, type: "text"},  
							{ display: "保质期", name: "inventory.expiraData", newline: false, type: "spinner",editor:{isNegative:false}},
							{ display: "保质期单位", name: "inventory.expiraDataUtil", textField:"inventory.expiraDataUtil",newline: false,  type: "select",editor: {data:shelf_life_unit,
								valueField: 'value',textField: 'text'
							}}
                    ]
                } 
            ]
        } */
    });
  
/*   //创建分录
    grid = $("#grid").ligerGrid({
        columns: [
	        { display: "目标计量单位", name: "baseUnitMeasure.uuid",width: 100, newline: false, type: "popup",textField:"baseUnitMeasure.name",
                        	   editor: {type: 'popup',
			                       grid: getBaseUnitMeasureGrid(true),
			                       valueField:'uuid',
			                       textField: 'name',
			                       //parms :{date:new Date().getTime(),test:1111},
			                       width: 300,
			                       height:100,
			                       realTimeLoad:true,
			                       onButtonClick:function(){
			                    	   console.log(this);
			                       }
			              	 }   
            },
	        { display: '换算率', name: 'conversionRate', width: 100,type: 'float', editor: { type: 'float' }}
        ], usePager: false,width: '99%',height: '98%',enabledEdit: true,alternatingRow:true,
        checkbox: true,headerRowHeight:22, rowHeight:22, rownumbers:true,
        onAfterEdit: f_onAfterEdit,

        toolbar: { items: [
                           { text: '添加行', click: addNewRow,icon: 'add' },
                           { line: true },
                           { text: '删除行', click: delRow,icon: 'delete' }
                           ]
                 }
        
    }); */
  
  if(existCustomer){
	  
	 
	//  grid.set({ data: {Rows:existCustomer.entrys}}); 
	  try{
		  if(!existCustomer.expiraDateManage || existCustomer.expiraDateManage ==false){
			  form1.setEnabled(["expiraDataUtil","expiraData"],false);
			  existCustomer.expiraData = null;
			  existCustomer.expiraDataUtil = null;
		  }else{
			  form1.setFieldValidate("expiraDataUtil",{required:true},{});
			  form1.setFieldValidate("expiraData",{required:true},{});
		  }
		  if(!existCustomer.barCodeManage || existCustomer.barCodeManage ==false){
			  form1.setEnabled(["barCode"],false);
			  existCustomer.barCodeManage = 'false';
		  }else{
			  form1.setFieldValidate("barCode",{required:true},{});
		  }
		  
		 if(!existCustomer.lotManage){
			  existCustomer.lotManage ='false';
		  }
		if(!existCustomer.barCodeManage){
			existCustomer.barCodeManage = 'false';
			  }
		if(!existCustomer.sequeManage){
			existCustomer.sequeManage = 'false';
		}
		if(!existCustomer.expiraDateManage){
			existCustomer.expiraDateManage ='false';
		}
		  form1.setData(existCustomer);
	  }catch(e){
		  console.log(e);
	  }
  }else{
	  form1.setEnabled(["expiraDataUtil","expiraData"],false);
	  form1.setFieldValidate("barCode",{required:true},{});
	  if(number){
		  var numberE = form1.getEditor("number"); //form中应用
		  numberE.setValue(number);
		 // form1.setData({number:number});
	  }
	  if(matCategoryUuid){
		  var e = form1.getEditor("matCategory.uuid"); //form中应用
		  e.setValue(matCategoryUuid,null);
	  }
  }

  if(status && status =='1'){
	  toolbar.setDisabled('save');
	  form1.setAllEnabled(false);
	 // grid.setEnabledEdit(false);
  }
});

//保存
function saveForm(){
   // var forms = $('#form1').serialize(); 
    var data = form1.getData();
    //var gridData = grid.getData();
  /*   delete data.baseUnitName;
    delete data.matCategoryName;
    if(gridData){
    	for(var i=0;i<gridData.length;i++){
        	delete gridData[i].baseUnitMeasure.name;
        }
    } */
   // data = JSON.stringify(data);
    console.log(data);
    if(form1.valid()){
    	//data.entrys = gridData;
	 	$.ajax({
	        loading: '正在保存数据中...',
	        type:'post',
	        url: "/material/saveOrUpdateMaterial",
			dataType:'json',
	        data: {material:JSON.stringify(data)},
	        success: function (data) {
					$.ligerDialog.success(data.message,'信息',function(){
						$("#uuid").val(data.data.uuid);
				    });
	        },
	        error: function (message) {
	       	 	$.ligerDialog.error("保存失败");
	        }
	    });
    }
}


</script>
</head>
<body>
	<div id="toptoolbar"></div>
	<form id="form1"></form>
	<!-- <div id="grid" style="margin: 8px;"></div> -->
</body>
<script type="text/javascript">
function addNewRow(){
	grid.add();
}
//删除分录
function delRow(){
	grid.deleteSelectedRow();
}
function f_onAfterEdit(e){
	
}
</script>
</html>
